import { camelCase, upperFirst } from 'lodash-es'
import { defineAsyncComponent } from 'vue'
// 历史记录，后续组件请写入business，会自动注册
import pLog from './pLog'
import pTable from './pTable'
import pSearch from './pSearch'
import pLayout from './pLayout'
import pUploadImg from './pUploadImg'
import ossUpload from './OssUpload'
import ViewImg from './ViewImg'
import columnTable from './columnTable/index.vue'
import pUploadExcel from './pUploadExcel'
import pRecord from './pRecord'

const components = import.meta.glob('./business/(**/*|*).vue')
// 全局组件的注册
const install = function (app) {
  Object.keys(components).forEach(fileName => {
    // 获取组件配置
    const file = components[fileName]
    // 获取组件的 PascalCase 命名
    const componentName = upperFirst(
      camelCase(
        // 获取和目录深度无关的文件名
        fileName
          .split('/')
          .pop()
          .replace(/\.\w+$/, '')
      )
    )
    // 全局注册组件
    app.component(`${componentName}`, defineAsyncComponent(file))
  })
  app
    .component('p-table', pTable)
    .component('p-search', pSearch)
    .component('p-log', pLog)
    .component('p-layout', pLayout)
    .component('p-upload-img', pUploadImg)
    .component('oss-upload', ossUpload)
    .component('columnTable', columnTable)
    .component('p-upload-excel', pUploadExcel)
    .component('ViewImg', ViewImg)
    .component('p-record', pRecord)
}
export default { install }
